package com.icss.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.service.IGoodsDbService;
import com.icss.service.impl.GoodsDbServiceImpl;
import com.icss.vo.GymCategory;
import com.icss.vo.GymCategoryEnum;
import com.icss.vo.GymGoods;
import com.icss.vo.GymGoodsEnum;
import com.icss.vo.GymMessage;
import com.icss.vo.GymMessageEnum;

/**
 *  首页操作
 * 
 * @author 郝厅
 */
@WebServlet(urlPatterns = "/IndexHeadServlet")
public class IndexHeadServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService ;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.goodsDbService = new GoodsDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("main".equalsIgnoreCase(method))
		{
			/* main */
			main(request, response);
		} else if ("header".equalsIgnoreCase(method))
		{
			/* header */
			header(request, response);
		}else if ("goodsList".equalsIgnoreCase(method))
		{
			/* goodsList */
			goodsList(request, response);
		} else if ("goodsInfo".equalsIgnoreCase(method))
		{
			/* goodsInfo */
			goodsInfo(request, response);
		}  else if ("about".equalsIgnoreCase(method))
		{
			/* about */
			about(request, response);
		} 
	}
	
	/**
	 * 首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", GymGoodsEnum.STATUS_UP.getStatus());
		/* 按照销量排名的前20件商品 */
		request.setAttribute("orderby", "saleNumDesc");
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<GymGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		String path = this.prefix + "/head/main" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * header
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void header(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 查询所有的分类 */
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", GymCategoryEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<GymCategory> apiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("headerResponse", apiResponse.toJSON());
		String path = this.prefix + "/head/header" + this.subfix; 
		request.getRequestDispatcher(path).include(request, response);
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void goodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", GymGoodsEnum.STATUS_UP.getStatus());
		
		/* 根据分类搜索 */
		String categoryId = request.getParameter("categoryId");
		if(categoryId == null)
		{
			categoryId = "" ; 
		}
		request.setAttribute("categoryId", categoryId);
		condMap.put("categoryId", categoryId);

		/* 根据dataType搜索 */
		String dataType = request.getParameter("dataType");
		if(dataType == null)
		{
			dataType = "" ; 
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);
		
		/* 根据分类搜索 */
		String orderby = request.getParameter("orderby");
		if(orderby == null)
		{
			orderby = "" ; 
		}
		request.setAttribute("orderby", orderby);
		condMap.put("orderby", orderby);
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<GymGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", GymCategoryEnum.STATUS_ENABLE.getStatus());
		ApiResponse<GymCategory> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		
		/* 页面跳转 */
		String path = "/head/goodsList" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void goodsInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<GymGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		/* 加载评论列表 */
		condMap.clear();
		condMap.put("goodsId", id);
		condMap.put("status", GymMessageEnum.STATUS_ENABLE.getStatus());
		ApiResponse<GymMessage> messageApiResponse = this.goodsDbService.findCondListMessageService(null, condMap);
		request.setAttribute("messageApiResponse", messageApiResponse.toJSON());
		/* 页面跳转 */
		String path = "/head/goodsInfo" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 关于我们
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void about(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 页面跳转 */
		String path = "/head/about" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}
